package com.vvlive.caibao.smartcity.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

public class ImageUtil {

	public static final int IMAGE_SHAP_OVAL = 0;
	public static final int IMAGE_SHAP_RECT = 1;
	/**
	 * 利用ImageAsyncLoader加载图片
	 * @param context	上下文对象
	 * @param imageUrl	图片的网络路径
	 * @param image		要显示图片的ImageView
	 * @param shape		图片的形状：0->圆形, 其他->方形
	 */
	public static void loadImage(final Context context, final String imageUrl, final ImageView image, final int shape) {
		ImageAsyncLoader asyncImageLoader = new ImageAsyncLoader();
		// 异步加载图片
		Drawable cachedImage = asyncImageLoader.loadDrawable(context,
				imageUrl, new ImageAsyncLoader.ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,
							String imageUrl) {
						if (imageDrawable != null) {
							Drawable drawable = ImageAsyncLoader
									.zoomDrawable(imageDrawable,
                                            ImageAsyncLoader.dip2px(context,
                                                    150), ImageAsyncLoader
                                                    .dip2px(context, 150));
							image.setImageBitmap(shape == 0 ? getRoundBitmap(drawable) : getRectBitmap(drawable));
						}
					}
				});
		if (cachedImage != null) {
			Drawable drawable = ImageAsyncLoader.zoomDrawable(cachedImage,
                    ImageAsyncLoader.dip2px(context, 150),
                    ImageAsyncLoader.dip2px(context, 150));
			image.setImageBitmap(shape == 0 ? getRoundBitmap(drawable) : getRectBitmap(drawable));
		}
	}

	public static void loadImage(Context context, String imageUrl, ImageView image){
		ImageAsyncLoader asyncLoader = new ImageAsyncLoader();
		Drawable cacheDrawable = asyncLoader.loadDrawable(context, imageUrl, new AsyncImageCallback(context, image));
		if(cacheDrawable != null){
			Drawable drawable = ImageAsyncLoader.zoomDrawable(cacheDrawable, ImageAsyncLoader.dip2px(context, 150), ImageAsyncLoader.dip2px(context, 150));
			image.setImageDrawable(drawable);
		}
	}

	private static final class AsyncImageCallback implements ImageAsyncLoader.ImageCallback{

		private Context context;
		private ImageView image;
		public AsyncImageCallback(Context context, ImageView image) {
			this.context = context;
			this.image = image;
		}

		@Override
		public void imageLoaded(Drawable imageDrawable, String imageUrl) {
			if(imageDrawable != null){
				Drawable drawable = ImageAsyncLoader.zoomDrawable(imageDrawable, ImageAsyncLoader.dip2px(context, 150), ImageAsyncLoader.dip2px(context, 150));
				image.setImageDrawable(drawable);
			}
		}
	}

	public static void loadImage(Context context, ImageView imageView, int shape){
		loadImage(context, (String) imageView.getTag(), imageView, shape);
	}
	
	/**
	 * 获得圆角位图
	 * @param drawable
	 * @return
	 */
	private static Bitmap getRoundBitmap(Drawable drawable){
		BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
		return RoundImageUtil.makeRoundCorner(bitmapDrawable.getBitmap());
	}
	
	/**
	 * 获得方形位图
	 * @param drawable
	 * @return
	 */
	private static Bitmap getRectBitmap(Drawable drawable){
		BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
		return bitmapDrawable.getBitmap();
	}

}
